﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoderJoe.WoW.Services;
using System.Windows;
using System.Windows.Media;
using CoderJoe.WoW.GearManager.Views;

namespace CoderJoe.WoW.GearManager
{
    internal class ItemToolTipController
        : Controller
    {
        private ItemToolTipView View { get; set; }

        public ItemToolTipController(Workspace workspace)
            : base(workspace)
        {
        }

        public void Show(int itemId, EquipmentButton button)
        {
            Hide();
            ItemService itemService = Workspace.Resolve<ItemService>();
            ItemDetail item = itemService.FindOrLoadItemDetail(itemId);
            ItemToolTipModel model = new ItemToolTipModel();
            //model.Load(item);

            model.Name = item.Name;
            model.Bonding = itemService.TranslateBondingToName(item.Bonding);
            model.ItemSlot = itemService.TranslateInventoryTypeIdToName(item.InventoryTypeId);
            model.ItemClass = item.SubClassName;
            model.NameColor = new SolidColorBrush(GetNameColor(item.QualityId));

            List<ItemToolTipAttributeModel> attributes = new List<ItemToolTipAttributeModel>();
            model.Attributes = attributes;

            if (item.Armor > 0)
            {
                ItemToolTipAttributeModel attributeModel = new ItemToolTipAttributeModel();
                attributeModel.Description = String.Format("{0} Armor", item.Armor);
                if (item.BonusArmor > 0)
                    attributeModel.DescriptionColor = new SolidColorBrush(GetNameColor(ItemQuality.Uncommon));
                attributes.Add(attributeModel);
            }

            if (item.BonusStamina > 0)
            {
                ItemToolTipAttributeModel attributeModel = new ItemToolTipAttributeModel();
                attributeModel.Description = String.Format("+{0} Stamina", item.BonusStamina);
                attributes.Add(attributeModel);
            }

            if (item.BonusIntellect > 0)
            {
                ItemToolTipAttributeModel attributeModel = new ItemToolTipAttributeModel();
                attributeModel.Description = String.Format("+{0} Intellect", item.BonusIntellect);
                attributes.Add(attributeModel);
            }

            View = new ItemToolTipView();

            Point topRight = button.PointToScreen(new Point(button.Width, 0));

            View.Model = model;
            View.Left = topRight.X;
            View.Top = topRight.Y;
            View.Show();
        }

        private Color GetNameColor(int quality)
        {
            return GetNameColor((ItemQuality)quality);
        }

        private Color GetNameColor(ItemQuality quality)
        {
            switch (quality)
            {
                case ItemQuality.Poor:
                    return (Color)App.Current.Resources["PoorQualityColor"];
                case ItemQuality.Common:
                    return (Color)App.Current.Resources["CommonQualityColor"];
                case ItemQuality.Uncommon:
                    return (Color)App.Current.Resources["UncommonQualityColor"];
                case ItemQuality.Rare:
                    return (Color)App.Current.Resources["RareQualityColor"];
                case ItemQuality.Epic:
                    return (Color)App.Current.Resources["EpicQualityColor"];
                case ItemQuality.Legendary:
                    return (Color)App.Current.Resources["LegendaryQualityColor"];
                case ItemQuality.Artifact:
                    return (Color)App.Current.Resources["ArtifactQualityColor"];
            }

            return Colors.White;
        }

        internal void Hide()
        {
            if (View != null)
            {
                View.Close();
                View = null;
            }
        }
    }
}
